package com.ziyun.erp.web.modules.order.controller;

import com.utility.New;
import com.ziyun.erp.common.utils.UserUtils;
import com.ziyun.erp.modules.logistics.dto.ErpLogisticsDto;
import com.ziyun.erp.modules.order.dto.OrderPaymentDto;
import com.ziyun.erp.modules.order.service.*;
import io.renren.common.utils.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;


/**
 * 订单基础信息表
 *
 * @author guizhao
 * @date 2018-08-03 10:40:47
 */
@RestController
@RequestMapping("order/orderinfo")
public class OrderInfoController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        // -1 : 全部 0: 紫云网  1：第三方
        String supplierType = (String) params.get("supplierType");
        if (StringUtils.isBlank(supplierType)){
            params.put("supplierType", 0);
        }
        return orderInfoService.queryPageOrderList(params);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{orderId}")
    public R info(@PathVariable("orderId") Long orderId){
        return orderInfoService.queryOrderInfo(orderId);
    }

    /**
     * 查询订单日志备注
     */
    @RequestMapping("/getOrderLogfo/{orderId}")
    public R getOrderLog(@PathVariable("orderId") Long orderId){
        return orderInfoService.getOrderLog(orderId);
    }

    /**
     * 查询订单生产信息
     */
    @RequestMapping("/getOrderProduction/{orderId}")
    public R getOrderProduction(@PathVariable("orderId") Long orderId){
        return orderInfoService.getOrderProduction(orderId);
    }

    /**
     * 查询订单物流信息
     */
    @RequestMapping("/getOrderLogi/{orderId}")
    public R getOrderLogi(@PathVariable("orderId") Long orderId){
        return orderInfoService.getOrderLogi(orderId);
    }

    /**
     * 查询订单设计师信息
     */
    @RequestMapping("/getOrderDesigner")
    public R getOrderDesigner(@RequestParam("name") String name){
        Map<String, Object> params = New.hashMap();
        params.put("name", name);
        return orderInfoService.getOrderDesigner(params);
    }

    /**
     * 订单备注
     */
    @RequestMapping("/orderRemarks")
    public R orderRemarks(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
        SysUserEntity user = UserUtils.getCurentUser();
        return orderInfoService.orderRemarks(orderId, remarks, user.getUsername());
    }

    /**
     * 订单作废
     */
    @RequestMapping("/orderInvalid")
    public R orderInvalid(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
        String username = UserUtils.getCurentUser().getUsername();
        return orderInfoService.orderInfoInvalid(orderId, remarks, username);
    }

    /**
     * 快捷订单作废
     */
    @RequiresPermissions("order:orderinfo:orderQuickInvalid")
    @RequestMapping("/orderQuickInvalid")
    public R orderQuickInvalid(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
        String username = UserUtils.getCurentUser().getUsername();
        return orderInfoService.orderQuickInvalid(orderId, remarks, username);
    }

    /**
     * 订单完成
     */
    @RequiresPermissions("order:orderinfo:orderfinish")
    @RequestMapping("/orderFinish")
    public R orderFinish(@RequestParam("orderId") String orderId, @RequestParam("remarks") String remarks){
        String username = UserUtils.getCurentUser().getUsername();
        return orderInfoService.orderInfoFinish(orderId, remarks, username);
    }

    /**
     * 订单支付
     */
    @RequestMapping("/orderPayment")
    public R orderPayment(@RequestBody OrderPaymentDto orderPaymentDto){
        ValidatorUtils.validateEntity(orderPaymentDto);
        return orderInfoService.orderPayment(orderPaymentDto);
    }

    /**
     * 订单调价
     */
    @RequestMapping("/orderDiscountAudit")
    public R orderDiscountAudit(@RequestParam("orderId") String orderId, @RequestParam("discount") String discount,
                                @RequestParam("remark") String remark){
        String username = UserUtils.getCurentUser().getUsername();
        return orderInfoService.orderInfoDiscountAudit(orderId, discount, remark, username);
    }

    /**
     * 删除
     */
    @RequestMapping("/adnormal")
    public R adnormal(@RequestParam("orderId") String orderId){
        orderInfoService.adnormal(orderId);

        return R.ok();
    }

    /**
     * 是否开发票
     */
    @RequestMapping("/orderTax")
    public R orderTax(@RequestParam("orderId") String orderId, @RequestParam("sendTax") Integer sendTax){
        return orderInfoService.orderTax(orderId, sendTax);
    }

    /**
     * 添加物流(设计商品)
     */
    @RequestMapping("/saveOrderLogisticsInfo")
    public R saveOrderLogisticsInfo(@RequestBody ErpLogisticsDto erpLogisticsDto){
        String username = UserUtils.getCurentUser().getUsername();
        return orderInfoService.saveDesinOrderLogisticsInfo(erpLogisticsDto, username);
    }

}
